package com.lobaby.tools.common.utils;

import org.apache.poi.ss.usermodel.DateUtil;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 *         Created by Administrator on 2020/12/28 0028.
 */
public class DateUtils {
    private final static org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DateUtil.class);
    /** 日期格式：yyyy-MM-dd HH:mm:ss */
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /** 日期格式：yyyyMMdd */
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
    /** 日期格式：yyyyMMddHHmmssSSS */
    public static final String DATE_FORMAT_YYYYMMDDSSS = "yyyyMMddHHmmssSSS";
    /** 日期格式：yyyyMMddHHmmss */
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    /** 日期格式：yyyy-MM-dd */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 字符串转date,默认格式yyyy-MM-dd HH:mm:ss
     *
     * @param source
     * @return
     */
    public static Date parseDate(String source)
    {
        return parseDate(source, "yyyy-MM-dd HH:mm:ss");
    }

    public static Timestamp getCurrentTimestamp(){
        Date currentTime = new Date();
        String date = formatDate(currentTime, DEFAULT_PATTERN);
        return  Timestamp.valueOf(date);
    }

    /**
     * 字符串转date
     *
     * @param source
     * @param pattern
     *            格式
     * @return
     */
    public static Date parseDate(String source, String pattern)
    {
        if (source == null || source.equals(""))
        {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try
        {
            return sdf.parse(source);
        }
        catch (ParseException e)
        {
            LOGGER.error("",e);
            return null;
        }
    }

    /**
     * 格式化日期,默认格式yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date)
    {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     *            格式
     * @return
     */
    public static String formatDate(Date date, String pattern)
    {
        if (date == null)
        {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 格式化当前的日期
     *
     * @param pattern
     * @return
     *
     */
    public static String formatToday(String pattern)
    {
        Date today = new Date();
        return formatDate(today, pattern);
    }

    /**
     * 获取当前日期
     * @param pattern  日期格式，默认：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentDate(String pattern) {
        if (pattern == null)
            pattern = DEFAULT_PATTERN;
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(currentTime);
    }

    public static String getLessDate(int len) {
        Date d=new Date();
        SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        return df.format(new Date(d.getTime() - len * 24 * 60 * 60 * 1000));
    }

    public static String getLessDate(int len, String pattern) {
        Date d=new Date();
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date(d.getTime() - len * 24 * 60 * 60 * 1000));
    }

    public static long getLessMinutes(Date first, Date end) {
        return (end.getTime() - first.getTime())/(1000 * 60);
    }

    public static String getAddDate(String date,int len, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            Date d = df.parse(date);
            return df.format(new Date(d.getTime() + len * 24 * 60 * 60 * 1000));
        } catch (ParseException e) {
            return null;
        }
    }

    public static String parttern2parttern(String date,String partternsrc,String partterntarget){
        if (date == null || date.equals(""))
        {
            return null;
        }
        SimpleDateFormat src = new SimpleDateFormat(partternsrc);
        SimpleDateFormat target = new SimpleDateFormat(partterntarget);
        try {
            return target.format(src.parse(date));
        } catch (ParseException e) {
            return null;
        }
    }

    public static long getLessHour(String first, String end) {
        first = "2020-01-01 " + first + ":00";
        end = "2020-01-01 " + end + ":00";

        long lessMinutes = getLessMinutes(parseDate(first), parseDate(end));
        return lessMinutes / 60;
    }

    public static long getLessMinutes(String first, String end) {
        first = "2020-01-01 " + first + ":00";
        end = "2020-01-01 " + end + ":00";

        long lessMinutes = getLessMinutes(parseDate(first), parseDate(end));
        return lessMinutes;
    }
}